﻿using System.Windows.Forms;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;

namespace QuanLyThongTinCoSo.Forms.PhanLoaiChiTietPhuongTien
{
    public partial class frmPhanLoaiChiTietPhuongTienCoSoList : frmList
    {
        public frmPhanLoaiChiTietPhuongTienCoSoList()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            var frm = new frmPhanLoaiChiTietPhuongTienCoSoDetail();
            frm.OnLoadData += LoadData;
            frm.ShowDialog();
        }

        protected override void LoadData()
        {
            var phanLoaiChiTietPhuongTienBiz = new PhanLoaiChiTietPhuongTienPCCCCoSoBiz();
            dgvPhanLoaiChiTiet.DataSource = null;
            dgvPhanLoaiChiTiet.Refresh();
            dgvPhanLoaiChiTiet.DataSource = phanLoaiChiTietPhuongTienBiz.GetAll();
            txtTen.Text = string.Empty;
        }

        protected override void Edit()
        {
            EditData();
        }

        private void EditData()
        {
            if (dgvPhanLoaiChiTiet.SelectedRows.Count > 0)
            {
                var selectedRow = dgvPhanLoaiChiTiet.SelectedRows[0];
                var id = int.Parse(selectedRow.Cells[0].Value.ToString());

                using (var fEdit = new frmPhanLoaiChiTietPhuongTienCoSoDetail(id))
                {
                    fEdit.OnLoadData += LoadData;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        private void dgvPhanLoaiChiTiet_SelectionChanged(object sender, System.EventArgs e)
        {
            var gv = (DataGridView)sender;
            foreach (DataGridViewRow row in gv.SelectedRows)
            {
                ID = int.Parse(row.Cells[0].Value.ToString());
                txtLoaiPhuongTien.Text = row.Cells[1].Value.ToString();
                txtTen.Text = row.Cells[2].Value.ToString();
            }
        }
    }
}
